package com.yhy.auth.service;


import com.yhy.common.security.config.SecurityMessageSource;
import com.yhy.common.security.constant.SecurityConstants;
import com.yhy.common.security.model.MyUser;
import com.yhy.common.security.service.MyUserDetailsService;
import com.yhy.common.security.util.PasswordEncoderUtil;
import com.yhy.common.security.util.SecurityUtil;
import lombok.RequiredArgsConstructor;
import org.assertj.core.util.Sets;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author jax-li
 */
@Service
@RequiredArgsConstructor
public class MyUserDetailsServiceImpl implements MyUserDetailsService {

    protected MessageSourceAccessor messages = SecurityMessageSource.getAccessor();

    @Override
    public UserDetails loadUserByUsername(String username) {
        List<GrantedAuthority> authorities = AuthorityUtils
                .createAuthorityList(Sets.newLinkedHashSet("admin", "test").toArray(new String[0]));
        // PasswordEncoderUtil和bean（passwordEncoder）的区别是utils生成的不带认证方式。
        // bean生成的默认是带{bcrypt}特征码的，因为实例化的时候有默认值
        final String password = SecurityConstants.BCRYPT + PasswordEncoderUtil.encode("123456");
        return new MyUser(1L, obtainClientId(), "andy", password, true, true, true, true, authorities);
    }

    @Override
    public UserDetails loadUserByUnionId(String unionId, String appId, String openId) {
        return null;
    }

    @Override
    public UserDetails loadUserByMobile(String mobile) {
        List<GrantedAuthority> authorities = AuthorityUtils.createAuthorityList(Sets.newLinkedHashSet("admin").toArray(new String[0]));
        final String clientId = obtainClientId();
        final String password = SecurityConstants.NOOP + PasswordEncoderUtil.encode("123456");
        return new MyUser(2L, obtainClientId(), "andy", password, true, true, true, true, authorities);
    }

//    private void check(SysUserBase vo) {
//
//        if (Objects.isNull(vo) || Objects.isNull(vo.getId())) {
//            throw new UsernameNotFoundException(messages.getMessage(
//                    "AbstractUserDetailsAuthenticationProvider.userNameNotFound",
//                    "User does not exist"));
//        }
//        // TODO 其他校验逻辑
//    }
//
//    private UserDetails conversion(SysUserBase user) {
//        Set<String> roles = user.getRoles();
//        List<GrantedAuthority> authorities = AuthorityUtils.createAuthorityList(roles.toArray(new String[0]));
//
//        return new MyUser(user.getId(),
//                user.getUserName(),
//                user.getPassword(),
//                user.getIsEnabled(),
//                true,
//                true,
//                !user.getIsLocked(),
//                authorities);
//    }

    /**
     * 获取clientId
     *
     * @return clientId
     */
    private String obtainClientId() {
        // 用户信息校验之前，SecurityContextHolder.getContext().getAuthentication() 存放的是clientid
        return SecurityUtil.getClientId();
    }


}